using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

using SlimDX;
using SlimDX.Direct3D9;

using miciv;
using miciv.Rendering;
using micivCore.Components;
using miciv.Geometry;

namespace micivCore.Data
{
    public class MapTileDefinition
    {
        protected CoreContext m_coreContext;
        public CoreContext CoreContext
        {
            get
            {
                return this.m_coreContext;
            }
        }

        protected string m_strID;
        public string ID
        {
            get
            {
                return this.m_strID;
            }
        }

        protected Hashtable m_htblGeometryDefinitions;
        public Hashtable GeometryDefinitions
        {
            get
            {
                return this.m_htblGeometryDefinitions;
            }
        }

        protected Hashtable m_htblLayers;
        public Hashtable Layers
        {
            get
            {
                return this.m_htblLayers;
            }
        }

        protected string m_strDefaultGeometry;
        public string DefaultGeometry
        {
            get
            {
                return this.m_strDefaultGeometry;
            }
        }

        public MapTileDefinition(CoreContext _coreContext)
        {
            this.m_coreContext = _coreContext;
            this.m_htblGeometryDefinitions = new Hashtable();
            this.m_htblLayers = new Hashtable();
        }

        public bool Initialize(XmlElement _xmlConfig)
        {
            this.m_strID = _xmlConfig.Attributes["id"].Value;
            this.InitializeDisplay(_xmlConfig.SelectSingleNode("display") as XmlElement);
            this.InitializeLayers(_xmlConfig.SelectSingleNode("layers") as XmlElement);
            return true;
        }

        protected void InitializeDisplay(XmlElement _xmlDisplay)
        {
            this.m_strDefaultGeometry = this.CoreContext.CoreManager.GetXmlAttributeValue(_xmlDisplay, "defaultgeometryid", "");
            XmlNodeList lstXmlGeometries = _xmlDisplay.SelectNodes("geometry");
            foreach (XmlElement xmlGeometry in lstXmlGeometries)
            {
                MapTileGeometryDefinition geometryDefinition = new MapTileGeometryDefinition(this);
                if (false != geometryDefinition.Initialize(xmlGeometry))
                {
                    this.m_htblGeometryDefinitions.Add(geometryDefinition.ID, geometryDefinition);
                    // if no default geometry has been explicitly set then take the first available one.
                    if (false != string.IsNullOrEmpty(this.m_strDefaultGeometry))
                    {
                        this.m_strDefaultGeometry = geometryDefinition.ID;
                    }
                }
            }
        }

        protected void InitializeLayers(XmlElement _xmlLayers)
        {
            XmlNodeList lstXmlLayers = _xmlLayers.SelectNodes("layer");
            foreach (XmlElement xmlLayer in lstXmlLayers)
            {
                MapTileLayer layer = new MapTileLayer(this);
                if (false != layer.Initialize(xmlLayer))
                {
                    this.m_htblLayers.Add(layer.ID, layer);
                }
            }
        }

        public void Release()
        {
            foreach (DictionaryEntry de in this.GeometryDefinitions)
            {
                (de.Value as MapTileGeometryDefinition).Release();
            }
            this.GeometryDefinitions.Clear();
        }
    }
}
